package hc.tool;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import hc.service.AService;

public class UtilityServiec extends AService {

	public static String DoctorPrix = "DOC";
	public static String AdminPrix = "ADM";
	public static String OffiecPrix = "OFC";
	public static String AssistancePrix = "AST";
	public static String PatientPrix = "PAT";

	public static String PatientSeq = "patientseq";
	public static String UserSeq = "USERSEQ";
	public static String AssistanceSeq = "assistanceseq";

	public static String ApptSeq = "apptsequence";
	public static String HospitalSeq = "hospitalseq";
	public static String DiagnosisSeq = "diagnosisseq";
	public static String DrugSeq = "drugSeq";

	public static int length = 5;

	public static String getUserID(int roleId) {
		String userId = null;
		switch (roleId) {
		case 1:
			userId = getAdminID();
			break;
		case 2:
			userId = getOfficeID();
			break;
		case 3:
			userId = getDoctorID();
			break;
		case 4:
			userId = getAssistanceID();
			break;
		case 5:
			userId = getPatientID();
			break;
		default:
			break;
		}

		return userId;
	}

	public static String getDoctorID() {
		return DoctorPrix + getSequenceID(UserSeq);
	}

	public static String getPatientID() {
		return PatientPrix + getSequenceID(PatientSeq);
	}

	public static String getOfficeID() {
		return OffiecPrix + getSequenceID(UserSeq);
	}

	public static String getAssistanceID() {
		return AssistancePrix + getSequenceID(AssistanceSeq);
	}
	
	public static int getDrugID() {
		return Integer.parseInt(getSequenceID(DrugSeq));
	}

	public static String getAdminID() {
		return AdminPrix + getSequenceID(UserSeq);
	}

	public static String getHostpitalSeqVal() {
		return HospitalSeq + ".nextval";
	}

	private static String getSequenceID(String sequenceName) {
		String Id = null;
		Statement sment = null;
		ResultSet rs = null;
		String sql = "select lpad ( " + sequenceName + " .nextval, " + length
				+ ", '0' ) id from dual";
		try {
			sment = connection.createStatement();
			rs = sment.executeQuery(sql);
			if (rs != null && rs.next()) {
				Id = rs.getString(1);
			}
			rs.close();
			sment.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				sment.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return Id;
	}

}
